package uit.edu.vn.Activity;

import java.util.ArrayList;
import java.util.List;

import uit.edu.vn.Model.myImage;
import uit.edu.vn.Utility.ImageListAdapter;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.provider.MediaStore.Images;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;

public class ImageActivity extends ListActivity{

	 /** Called when the activity is first created. */
    private Cursor myCursor;
    ListView listview;
	private Handler handler;
	private ProgressDialog loadingImageDialog;
    private final List<myImage> listImage = new ArrayList<myImage>();
    private ArrayList<Boolean> itemImageChecked=new  ArrayList<Boolean>(); 
    private ArrayList<String> itemImageString= new ArrayList<String>();
    private ImageListAdapter iAdapter;
    private final int menu_Logout=0;
    private final int menu_Backup=1;
    private final int menu_MarkAll=2;
    
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.backup_image);
        
        //thao tac voi list
        listview =getListView();
        listview.setChoiceMode(2);
        //goi Dialog
	    loadingImageDialog = ProgressDialog.show(ImageActivity.this, "","Đang tải dữ liệu ...", true);	 
      //goi thread chay ngam voi UI
		handler= new Handler();
	    Thread myImageThread= new Thread(null,BackGroundThread,"Background");
	    myImageThread.start();
	           
    }
    private Runnable BackGroundThread = new Runnable() {
		
		@Override
		public void run() {
			// TODO Auto-generated method stub
			doinBackground();
		}
	};
	private void doinBackground()
	{
		 //tao con tro luu database
        myCursor=this.managedQuery(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
        startManagingCursor(myCursor);
        while (myCursor.moveToNext())
        {
            String imageName = myCursor.getString(myCursor.getColumnIndexOrThrow(Images.Media.DISPLAY_NAME));
            String imageSize= myCursor.getString(myCursor.getColumnIndexOrThrow(Images.Media.SIZE));
            String imageIcon= myCursor.getString(myCursor.getColumnIndexOrThrow(Images.Media.DATA));
            listImage.add(new myImage(imageName, imageSize, imageIcon));
        }
        handler.post(doUpdate);
	}
	private Runnable doUpdate = new Runnable() {
		
		@Override
		public void run() {
			// TODO Auto-generated method stub
			doUpdateUI();
		}
	};
	private void doUpdateUI()
	{
		// cap nhat lai GUI
		iAdapter = new ImageListAdapter(this,listImage);
        this.setListAdapter(iAdapter);
        // xoa dialog
        loadingImageDialog.dismiss();
	}
    //xu lý menu
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	// TODO Auto-generated method stub
    	//return super.onCreateOptionsMenu(menu);
    	 menu.add(0, menu_Logout, 0,"Đăng Xuất").setIcon(R.drawable.ic_error);
    	 menu.add(0, menu_Backup, 0,"Sao Lưu").setIcon(R.drawable.ic_backup_select);
    	 menu.add(0, menu_MarkAll,0, "Chon Hết").setIcon(R.drawable.ic_checkall);
		return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	// TODO Auto-generated method stub
    	switch(item.getItemId())
    	{
    		case menu_Backup:
    			//xu ly backup
    			itemImageString.clear();
    			String path= null;
    			for(int i=0;i<itemImageChecked.size();i++)
    			{
    				// kiem tra mang item check , neu la
    				//true thi lay vi tri va truyen vao 
    				//cursor
    				if(itemImageChecked.get(i)==true)
    				{
    	    	        int index = myCursor.getColumnIndexOrThrow(Images.Media.DATA);
    	    	        myCursor.moveToPosition(i);
    	    	        path=myCursor.getString(index);
    	    	        //add vao mang itemString
    					itemImageString.add(path);
    					Log.d("Chuoi", path); 
    				}
    			}
    		case menu_Logout:
    			//xu ly logout
    			finish();
    			return true;
    		case menu_MarkAll:
    			//xu ly check all
    			// duyet mang item check va set true het
    			for(int i=0;i<itemImageChecked.size();i++)
    		       {
    				  itemImageChecked.set(i, true);
    		    	  iAdapter.setCheckList(itemImageChecked);
    		    	  
    		       }
    			this.setListAdapter(iAdapter);
    			return true;
    	}
    	return false;
    }
	
}
